package com.mychat.controller;

import com.mychat.annotation.GlobalInterceptor;
import com.mychat.entity.Constants;
import com.mychat.entity.dto.TokenUserInfoDto;
import com.mychat.entity.po.UserInfo;
import com.mychat.entity.vo.ResponseVO;
import com.mychat.entity.vo.UserInfoVO;
import com.mychat.enums.UserContactStatusEnum;
import com.mychat.enums.VerifyRegexEnum;
import com.mychat.exception.BusinessException;
import com.mychat.service.UserInfoService;
import com.mychat.utils.CopyTools;
import com.mychat.utils.StringUtils;
import com.mychat.websocket.ChannelContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.IOException;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private ChannelContextUtils channelContextUtils;

    @RequestMapping("/getUserInfo")
    @GlobalInterceptor
    public ResponseVO getUserInfo(HttpServletRequest request) throws BusinessException {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        UserInfo userInfo = this.userInfoService.getUserInfoByUserId(tokenUserInfoDto.getUserId());
        UserInfoVO userInfoVO = CopyTools.copy(userInfo, UserInfoVO.class);
        userInfoVO.setAdmin(tokenUserInfoDto.getAdmin());
        return getSuccessResponseVO(userInfoVO);
    }

    @RequestMapping("/updatePassword")
    @GlobalInterceptor
    public ResponseVO updatePassword(HttpServletRequest request, @NotEmpty @Pattern(regexp = Constants.REGEX_PASSWORD) String password) throws BusinessException, IOException {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);

        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(StringUtils.encodeMd5(password));
        this.userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfoDto.getUserId());

        // 强制退出,重新登录
        channelContextUtils.closeContent(tokenUserInfoDto.getUserId());

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/saveUserInfo")
    @GlobalInterceptor
    public ResponseVO saveUserInfo(HttpServletRequest request, UserInfo userInfo, MultipartFile avatarFile, MultipartFile avatarCover) throws BusinessException, IOException {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        userInfo.setUserId(tokenUserInfoDto.getUserId());
        userInfo.setPassword(null);
        userInfo.setStatus(null);
        userInfo.setCreateTime(null);
        userInfo.setLastLoginTime(null);

        this.userInfoService.updateUserInfo(userInfo, avatarFile, avatarCover);

        return getUserInfo(request);
    }

    @RequestMapping("/logout")
    @GlobalInterceptor
    public ResponseVO logout(HttpServletRequest request) throws BusinessException, IOException {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);

        // 强制退出,重新登录 关闭ws链接
        channelContextUtils.closeContent(tokenUserInfoDto.getUserId());

        return getSuccessResponseVO(null);
    }
}
